<?php
/**
 * @author Lukas Normantas
 */
class InstallController extends Areal_Controller_Action
{

    /**
     * Index page
     */
    public function indexAction()
    {
        $form = new Default_Form_Installation();
        if ($this->getRequest()->isPost()){
            if ($form->isValid($this->getRequest()->getPost())){
                
                new Default_Model_User();
                new Default_Model_UserRegistrationConfirm();
                new Default_Model_UserPasswordChange();
                new Default_Model_UserEmailChange();

                new Catalog_Model_Category();
                new Catalog_Model_Product();
                new Catalog_Model_Special();

                new Content_Model_Page();
                new Content_Model_Menu();
                new Content_Model_Album();
                new Content_Model_Picture();

                $conn = Doctrine_Manager::getInstance()->getCurrentConnection();
                $models = Doctrine::getLoadedModels();
                foreach($models as $model) {
                    $table_name = Doctrine::getTable($model)->getTableName();
                    try{
                        $conn->export->dropTable($table_name);
                    } catch(Exception $e){ /*echo 'problem with droping table '. $table_name ."\n";*/}
                }

                Doctrine::createDatabases();
                Doctrine::createTablesFromArray($models);

                $installation_type = $form->getValue('installation_type');
                if ($installation_type == 'demo'){
                    Doctrine::loadData(APPLICATION_PATH . 'db/data-2009-05-31_0916.yml');
                }
                if ($installation_type == 'ufo'){
                    Doctrine::loadData(APPLICATION_PATH . 'db/ufo.yml');
                }

            }
        }
        $this->view->form = $form;
    }
}
